package com.xiaohui.gwc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohui.gwc.mapper.ProductsMapper;
import com.xiaohui.gwc.mapper.UPMapper;
import com.xiaohui.gwc.mode.*;
import com.xiaohui.gwc.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Poppin
 * @Date: 2021/11/30 12:40
 * @Version: 1.0
 */
@Service
public class ProductsServiceImpl implements ProductsService {


    private ProductsMapper productsMapper;

    private UPMapper upMapper;

    @Autowired
    private ProductsServiceImpl(ProductsMapper productsMapper,UPMapper upMapper){
        this.productsMapper = productsMapper;

        this.upMapper=upMapper;
    }

    /**
     * 查询所有的商品信息
     * @return
     */
    @Override
    public Result getAllProduct(Integer current) {
        ProductsVo productsVo = new ProductsVo();

        Page pageinfo = new Page(current,16);
        IPage<Products> productsIPage;
        productsIPage = productsMapper.selectPage(pageinfo,null);
        List<Products> products;
        products = productsIPage.getRecords();
        productsVo.setCurrent(current);
        productsVo.setTotal(productsIPage.getTotal());
        productsVo.setPages(productsIPage.getPages());
        productsVo.setSize(productsIPage.getSize());
        productsVo.setProductsList(products);
        return new Result<ProductsVo>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(),"成功",productsVo
        );
    }

    /**
     * 根据商品分类查询
     * @param cname     分类名称
     * @return
     */
    @Override
    public Result getCategory(String cname) {
        List<Products> products;
        products=productsMapper.getCategory(cname);
        return new Result<List>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "成功",products
        );
    }

    @Override
    public Result getLikeProduct(String name) {

        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.like("pname",name);
        List<Products> products;
        products = productsMapper.selectList(wrapper);
        return new Result<List>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "成功",products
        );
    }

    @Override
    public Result Upload(Upload upload) {
        upload.setPid(null);
        int result=upMapper.insert(upload);
        if (result!=0){
            return new Result<AffectedRows>(
                    ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "注册成功",new AffectedRows(result)
            );
        }else {
            return new Result<AffectedRows>(
                    ReturnCode.ERROR.getCode(), ReturnCode.ERROR.getType(), "注册成功",new AffectedRows(result)
            );
        }
    }

    @Override
    public Result getRdAll() {
        List<Products> products;
        products = productsMapper.getRd();
        return new Result<List>(
                ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getType(), "成功",products
        );
    }


}

